/*
 * QNotebookListItemModel.h
 *
 *  Created on: Sep 17, 2010
 *      Author: alex
 */

#ifndef QNOTEBOOKLISTITEMMODEL_H_
#define QNOTEBOOKLISTITEMMODEL_H_

#include <QAbstractListModel>
#include <QVector>
#include "QvernoteAPI.h"

class QNotebookListItemModel : public QAbstractListModel
{
	Q_OBJECT

public:
	QNotebookListItemModel(const QString& filterString = "", QObject* parent = 0)
	: QAbstractListModel(parent) {
		api = QvernoteAPI::Instance();
		Notebook notebook;
		bool hasNextNotebook;

		for(hasNextNotebook = api->getFirstNotebook(notebook); hasNextNotebook; hasNextNotebook = api->getNextNotebook(notebook))
		{
			if(QString::fromStdString(notebook.name).startsWith(filterString, Qt::CaseInsensitive))
			{
				notebookList.append(notebook);
				notebookMarkList.append(false);
			}
		}
	}

	int rowCount(const QModelIndex &index = QModelIndex()) const {
		return notebookList.size();
	}

	QVariant data(const QModelIndex &index, int role) const {
		if (role == Qt::DisplayRole) {
			const Notebook* nPtr = &notebookList[index.row()];
			return QVariant((int)nPtr);
		}

                else if(role == Qt::CheckStateRole) {
                    return (notebookMarkList[index.row()])? Qt::Checked : Qt::Unchecked;
                }

		return QVariant();
	}

        Qt::ItemFlags flags ( const QModelIndex & ) const {
            return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsUserCheckable;
        }

	Notebook& getNotebook(const QModelIndex& index) {
		return notebookList[index.row()];
	}

	void toggleMark(const QModelIndex &index = QModelIndex()) {
		notebookMarkList[index.row()] = !notebookMarkList[index.row()];
		if(notebookMarkList[index.row()] == true)
			emit notebookMarked();
		else
			emit notebookUnmarked();
	}

	bool isMarked(const QModelIndex& index) {
		return notebookMarkList[index.row()];
	}

	void addAllNotebooksItem() {
		Notebook specialAllNotebook;
		specialAllNotebook.name = "All notebooks";
		notebookList.append(specialAllNotebook);
		notebookMarkList.append(false);
	}

signals:
	void notebookMarked();
	void notebookUnmarked();

private:
	QvernoteAPI* api;
	QVector<Notebook> notebookList;
	QVector<bool>	  notebookMarkList;
};

#endif /* QNOTEBOOKLISTITEMMODEL_H_ */
